package j2025.j03;

import java.util.Arrays;

/**
 * @Author 猫大仙
 */
public class j0309 {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        int[] ret = new int[n];
        for (int i = 0; i < n; i++) {
            ret[(i + k) % n] = nums[i];
        }
        System.arraycopy(ret, 0, nums, 0, n);
    }


    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];
        ret[0] = 1;
        for (int i = 1; i < n; i++) {
            ret[i] = ret[i-1]*nums[i-1];
        }
        int r = 1;
        for (int i = n-1; i >=0 ; i--) {
            ret[i] = ret[i]*r;
            r*=nums[i];
        }
        return ret;
    }
    public int[] productExceptSelf2(int[] nums) {
        int n = nums.length;
        int l = 0,r = n-1;
        int left = 1,right = 1;
        int[] ret = new int[n];
        Arrays.fill(ret,1);
        while(l<n && r>=0){
            ret[l] = ret[l]*left;
            ret[r] = ret[r]*right;
            left*=nums[l++];
            right*=nums[r--];
        }
        return ret;
    }
}
